<!DOCTYPE html>
<html lang="zh-CN">
<head>
    {% load static %}
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <title>用户管理界面</title>

</head>
<body>
<div class="container-fluid">
    <div class="row">
        {% include 'component/navbarComponent.html' %}
        <div class="col-sm-9 col-md-10 main">
            <!-- 内容区域 -->
            <div class="row">
                <div class="col-md-6">
                    <button id="btn-add" class="btn btn-primary">添加用户</button>
                </div>
            </div>
            <br>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>用户名</th>
                    <th>密码</th>
                    <th>身份</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for datum in data %}
                    <tr>
                        <td>{{ datum.0 }}</td>
                        <td>{{ datum.1 }}</td>
                        <td>{{ datum.2 }}</td>
                        <td>
                            <button class="btn btn-sm btn-primary " data-toggle="modal"
                                    data-target="#edit-Modal" onclick="user_modify(this)">
                                修改
                            </button>
                            <button class="btn btn-sm btn-danger" onclick="del_user(this)">删除</button>
                        </td>
                    </tr>
                {% endfor %}

                </tbody>
            </table>

            <!-- 用户添加模态框 -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">添加用户</h4>
                        </div>
                        <div class="modal-body">
                            <!-- 在这里添加模态框的内容 -->
                            <form id="add-user-form">
                                <div class="form-group">
                                    <label for="username">username</label>
                                    <input type="text" class="form-control" id="username" name="username"
                                           placeholder="用户名">
                                </div>
                                <div class="form-group">
                                    <label for="password">password</label>
                                    <input type="text" class="form-control" id="password" name="password"
                                           placeholder="用户密码">
                                </div>
                                <div class="form-group">
                                    <label for="type">type</label>
                                    <input type="text" class="form-control" id="type" name="type"
                                           placeholder="用户身份">
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" id="btn-user-add">保存</button>
                        </div>
                    </div>
                </div>
            </div>

            <!--用户修改模态框-->
            <div class="modal fade" id="edit-Modal" tabindex="-1" role="dialog" aria-labelledby="edit-model-label">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="edit-model-label">请基于用户名修改用户信息</h4>
                        </div>
                        <div class="modal-body">
                            <!-- 在这里添加模态框的内容 -->
                            <form id="modify-user-form">
                                <div class="form-group">
                                    <label for="new_username">username</label>
                                    <input type="text" class="form-control" id="new_username" name="new_username">
                                </div>
                                <div class="form-group">
                                    <label for="new_password">password</label>
                                    <input type="text" class="form-control" id="new_password" name="new_password">
                                </div>
                                <div class="form-group">
                                    <label for="new_type">type</label>
                                    <input type="text" class="form-control" id="new_type" name="new_type">
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" id="btn-modify-user"
                                    onclick="btn_modify_user()">保存
                            </button>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

{#模态框处理js#}
<script>
    $(document).ready(function () {
        // 添加按钮点击事件
        $("#btn-add").on("click", function () {
            // 显示模态框
            $('#myModal').modal('show');
        });

        // 查找按钮点击事件
        $("#btn-search").on("click", function () {
            // 查找逻辑代码
            console.log("点击了查找按钮");
        });

        // 修改按钮点击事件
        $(".btn-edit").on("click", function () {
            // 显示模态框
            $('#edit-Modal').modal('show');
        });

        // 保存按钮点击事件
        $("#btn-user-add").on("click", function () {
            // 保存逻辑代码
            var formdata = new FormData(document.getElementById("add-user-form"))

            // 发送请求到后台
            axios.post("{% url 'add_user' %}", formdata)
                .then(function (response) {
                    alert(response.data.data)
                })
                .catch(function (error) {
                    // 处理请求错误
                    console.log(error);
                });
        });


    })
    ;
</script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>


{#用户信息处理js#}
<script>

    // 点击修改用户信息时的按钮
    function user_modify(button) {
        // 找到触发按钮所在的行
        let row = button.closest('tr');
        let username = row.cells[0].innerText;
        let modal_username = document.getElementById("new_username")
        modal_username.value = username
        modal_username.disabled = true
    }

    // 提交用户修改
    function btn_modify_user() {
        var formdata = new FormData(document.getElementById("modify-user-form"))
        formdata.append("new_username", document.getElementById("new_username").value)
        // 发送请求到后台
        axios.post("{% url 'modify_user' %}", formdata)
            .then(function (response) {
                // 处理后台返回的响应
                alert(response.data.data);
            })
            .catch(function (error) {
                // 处理请求错误
                console.log(error);
            });

    }

    // 删除用户
    function del_user(button) {
        // 找到触发按钮所在的行
        let row = button.closest('tr');
        let username = row.cells[0].innerText;
        // 发送请求到后台
        axios.post("{% url 'del_user' %}", {username: username})
            .then(function (response) {
                // 处理后台返回的响应
                alert(response.data.data);
            })
            .catch(function (error) {
                // 处理请求错误
                console.log(error);
            });
    }
</script>

</body>
</html>
